package com.hc.java.hash;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Created by hyp on 2018/5/22.
 */
public class Base64Utils {

    private final static byte[] EMPTY_BYTES = new byte[0];

    private final static Base64.Encoder encoder = Base64.getEncoder();

    private final static Base64.Decoder decoder = Base64.getDecoder();

    /**
     * base64编码
     *
     * @param bytes 内容数组
     * @return base64编码后的字符串
     */
    public static byte[] encodeToBytes(byte[] bytes) {
        if (null == bytes) {
            return null;
        }

        if (0 == bytes.length) {
            return EMPTY_BYTES;
        }

        return encoder.encode(bytes);
    }

    /**
     * base64编码
     *
     * @param text 内容字符串
     * @param charset text的字符集
     * @return base64编码后的字符串
     */
    public static byte[] encodeToBytes(String text, Charset charset) {
        if (null == text) {
            return null;
        }

        if (text.isEmpty()) {
            return EMPTY_BYTES;
        }

        if (null == charset) {
            charset = StandardCharsets.UTF_8;
        }

        return encoder.encode(text.getBytes(charset));
    }

    /**
     * base64编码
     *
     * @param text 内容字符串
     * @return base64编码后的字符串
     */
    public static byte[] encodeToBytes(String text) {
        return encodeToBytes(text, StandardCharsets.UTF_8);
    }

    /**
     * base64编码
     *
     * openssl验证:
     *     命令: echo -n "message" | openssl base64 -e
     *     注意: echo如果不加-n选项, 会自动添加换行符, 导致结果不正确
     *
     * @param bytes 内容数组
     * @return base64编码后的字符串
     */
    public static String encodeToString(byte[] bytes) {
        byte[] encodedBytes = encodeToBytes(bytes);
        return new String(encodedBytes);
    }

    /**
     * base64编码
     *
     * openssl验证:
     *     命令: echo -n "message" | openssl base64 -e
     *     注意: echo如果不加-n选项, 会自动添加换行符, 导致结果不正确
     *
     * @param text 内容字符串
     * @param charset text的字符集
     * @return base64编码后的字符串
     */
    public static String encodeToString(String text, Charset charset) {
        byte[] encodedBytes = encodeToBytes(text, charset);
        return new String(encodedBytes);
    }

    /**
     * base64编码
     *
     * openssl验证:
     *     命令: echo -n "message" | openssl base64 -e
     *     注意: echo如果不加-n选项, 会自动添加换行符, 导致结果不正确
     *
     * @param text 内容字符串
     * @return base64编码后的字符串
     */
    public static String encodeToString(String text) {
        return encodeToString(text, StandardCharsets.UTF_8);
    }

    /**
     * base64解码
     *
     * @param bytes base64编码的数组
     * @return 解码后的内容字符串
     */
    public static byte[] decodeToBytes(byte[] bytes) {
        if (null == bytes) {
            return null;
        }

        if (0 == bytes.length) {
            return EMPTY_BYTES;
        }

        return decoder.decode(bytes);
    }

    /**
     * base64解码
     *
     * openssl验证:
     *     命令: echo "message" | openssl base64 -d
     *
     * @param text base64编码的字符串
     * @param charset text的字符集
     * @return 解码后的内容字符串
     */
    public static byte[] decodeToBytes(String text, Charset charset) {
        if (null == text) {
            return null;
        }

        if (text.isEmpty()) {
            return EMPTY_BYTES;
        }

        if (null == charset) {
            charset = StandardCharsets.UTF_8;
        }

        return decoder.decode(text.getBytes(charset));
    }

    /**
     * base64解码
     *
     * openssl验证:
     *     命令: echo "message" | openssl base64 -d
     *
     * @param text base64编码的字符串
     * @return 解码后的内容字符串
     */
    public static byte[] decodeToBytes(String text) {
        return decodeToBytes(text, StandardCharsets.UTF_8);
    }

    /**
     * base64解码
     *
     * openssl验证:
     *     命令: echo "message" | openssl base64 -d
     *
     * @param bytes base64编码的数组
     * @return 解码后的内容字符串
     */
    public static String decodeToString(byte[] bytes) {
        byte[] decodedBytes = decodeToBytes(bytes);
        return new String(decodedBytes);
    }

    /**
     * base64解码
     *
     * openssl验证:
     *     命令: echo "message" | openssl base64 -d
     *
     * @param text base64编码的字符串
     * @param charset text的字符集
     * @return 解码后的内容字符串
     */
    public static String decodeToString(String text, Charset charset) {
        byte[] decodedBytes = decodeToBytes(text, charset);
        return new String(decodedBytes);
    }

    /**
     * base64解码
     *
     * openssl验证:
     *     命令: echo "message" | openssl base64 -d
     *
     * @param text base64编码的字符串
     * @return 解码后的内容字符串
     */
    public static String decodeToString(String text) {
        return decodeToString(text, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
//        String txt = "NobwRADghgTlC2YBc4AuUBG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BGD3HHjXifPeKFTwtiD6fjfZeJ1nyXe3rcy9t/nigDui8l8ExQV37Otec6QC973b3jf9vx7Xwnf2m/m6bCHm3Ye7dd6jxMMgf7txGAH/dj3yAjA19W/WqfR/G9m4lxbkHOel954j6vmHf7VQ6EmMzm8ABdOzNgIibFUeotWJErFtFtZdZaIDYGJXE+VTY2ILYvErZuJbYwZ7ZAklVZAQlEYwkiFPY5IolFI/ZVJA4NIQ5tJw49Io5DJY4TIE5zJLIU4bJ047Im5CdnJSkC4jhPIS5Tkr5zlK4gpq5QpwoG5Wls42DW4VlO5uot5e5d5BpB5CoRkJkypaUh4pk5kKQmCGoFkl5lVlkulxDN4e4+o+494hplVdkuDakeDNRHZlUTlqly4DlDwNoSUn47lDo34P5zpv5ro/4AEZEgFHpECwE/lMxIEAYwAKUfoQVQj4FS0kFQZQEIY4UoZEUsFkVQlkZMCMV5xFxyE8ZgcaxFUCVl1jxGFrlmFEDWEyYqVGYp5uEbAtEOVOZQI7EeVBY4IBEe1hUbkFER0lYVY6UNF1ZACtZdF9FnFwCmITZWJzZpBGA4CbZeJ/EOCUDW4UUCF3ZUZZIZBvZcDlI4kCDg4tIw5dJI4DIY5jJ44zIk4rJU5bJM5GDZkSlXJC4BhKklozkLDpoGl+DmlBCoo2lYogpOleRVkJCDD+ktkTCh55Cx4p5xlYTJkZ41DZkNCGQtDZQdCQS9DekNkjCZCD53jzDVpporD5AlVZRbCj5PjAo75yYbldpXCHkTozov5Lof4bp/47ovloVfk3p/lojoFKUOFQUwj484icEEi0EkiEVcEKFBxsERw1iMD0UrVMUcjsU5T1x8iwBCi6ESYSjiUyjSUWEIi6YoiaVwivx6jADGiBFmthEwABYxFhYJFOiZEpZujmhFFUdlF+i/9LEGUgCqI38gA=";
//        System.out.println(decodeToString(txt));
        System.out.println(encodeToString("abcdefg"));
    }
}
